home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / interapplication comm / 7edit / source / svappleevents.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.9 KB  |  304 lines

  1. /*
  2.     File:        SVAppleEvents.c
  3.  
  4.     Contains:    
  5.  
  6.     Written by:     Original version by Jon Lansdell and Nigel Humphreys.
  7.                                 3.1 updates by Greg Sutton.
  8.  
  9.     Copyright:    Copyright © 1995-1999 by Apple Computer, Inc., All Rights Reserved.
  10.  
  11.                             You may incorporate this Apple sample source code into your program(s) without
  12.                             restriction. This Apple sample source code has been provided "AS IS" and the
  13.                             responsibility for its operation is yours. You are not permitted to redistribute
  14.                             this Apple sample source code as "Apple sample source code" after having made
  15.                             changes. If you're going to re-distribute the source, we require that you make
  16.                             it clear in the source that the code was descended from Apple sample source
  17.                             code, but that you've made changes.
  18.  
  19.     Change History (most recent first):
  20.                             7/20/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  21.                 
  22.  
  23. */
  24. #include "SVAppleEvents.h"
  25.  
  26. #include <Resources.h>
  27.  
  28. #include "SVEditGlobals.h"
  29. #include "SVEditUtils.h"
  30. #include "SVEditAEUtils.h"
  31. #include "SVEditWindow.h"
  32. #include "SVEditFile.h"
  33.  
  34. #include "SVAERecording.h"
  35.  
  36. #include "SVAECompare.h"
  37. #include "SVAECountElements.h"
  38. #include "SVAEAccessors.h"
  39. #include "SVAECoercions.h"
  40. #include "SVAEObjectsExist.h"
  41. #include "SVAECreate.h"
  42. #include "SVAEClone.h"
  43. #include "SVAEMove.h"
  44. #include "SVAEDelete.h"
  45. #include "SVAECopy.h"
  46. #include "SVAECut.h"
  47. #include "SVAEPaste.h"
  48. #include "SVAESelect.h"
  49. #include "SVAEClose.h"
  50. #include "SVAESave.h"
  51. #include "SVAERevert.h"
  52. #include "SVAEGetData.h"
  53. #include "SVAESetData.h"
  54. #include "SVAEGetDataSize.h"
  55.  
  56. short        gRefNum;
  57.  
  58. #pragma segment Main
  59.  
  60. /*-----------------------------------------------------------------------*/
  61. /**----------                         APPLE EVENT HANDLING                     ---------------**/
  62. /*-----------------------------------------------------------------------*/
  63.  
  64.  
  65. // -----------------------------------------------------------------------
  66. //        Name:             DoAppleEvent
  67. //        Purpose:        Process and despatch the AppleEvent
  68. // -----------------------------------------------------------------------
  69.  
  70. void DoAppleEvent(EventRecord theEvent)
  71. {
  72.     OSErr err;
  73.  
  74.   // should check for your own event message types here - if you have any
  75.     
  76.     err = AEProcessAppleEvent(&theEvent);
  77. }
  78.  
  79. // -----------------------------------------------------------------------
  80. //        Name:             DoOpenApp
  81. //        Purpose:        Called on startup, creates a new document.
  82. // -----------------------------------------------------------------------
  83.  
  84. pascal OSErr DoOpenApp(const AppleEvent *message,const AppleEvent *reply,long refcon)
  85.   {
  86. #pragma unused (reply,refcon, message)
  87.     
  88.       DPtr ourDoc;
  89.     
  90.         /*just create a new document*/
  91.         ourDoc = NewDocument(false, (WindowPtr)-1L);
  92.         
  93.         if (ourDoc)
  94.             {
  95.                 ShowWindow(ourDoc->theWindow);
  96.                 return(noErr);
  97.             }
  98.         else
  99.             return(-108);
  100.     }
  101.  
  102. // -----------------------------------------------------------------------
  103. //        Name:             DoOpenDocument
  104. //        Purpose:        Open all the documents passed in the Open AppleEvent.
  105. // -----------------------------------------------------------------------
  106.  
  107. pascal OSErr DoOpenDocument(const AppleEvent *message, const AppleEvent *reply, long refcon)            
  108.   {
  109. #pragma unused (reply, refcon)
  110.  
  111.         long        index;
  112.         long        itemsInList;
  113.         AEKeyword   keywd;
  114.         OSErr       err;
  115.         OSErr       ignoreErr;
  116.         AEDescList  docList;
  117.         long        actSize;
  118.         DescType    typeCode;
  119.         FSSpec      theFSSpec;
  120.         
  121.         /*open the specified documents*/
  122.         
  123.         docList.dataHandle = nil;
  124.         
  125.         err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList);
  126.         
  127.         if (err==noErr)
  128.             err = AECountItems( &docList, &itemsInList) ;
  129.         else
  130.           itemsInList = 0;
  131.             
  132.         for (index = 1; index <= itemsInList; index++)
  133.             if (err==noErr)
  134.                 {
  135.                     err = AEGetNthPtr( &docList, index, typeFSS, &keywd, &typeCode,
  136.                                                          (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize ) ;
  137.                     if (err==noErr)
  138.                       err = OpenOld(theFSSpec);
  139.                 }
  140.     
  141.       if (docList.dataHandle)
  142.             ignoreErr = AEDisposeDesc(&docList);
  143.             
  144.         return(err);
  145.     }
  146.  
  147. // -----------------------------------------------------------------------
  148. //        Name:             MyQuit
  149. //        Purpose:        Quit event received- exit the program.
  150. // -----------------------------------------------------------------------
  151.  
  152. pascal OSErr MyQuit(const AppleEvent *message,const AppleEvent *reply,long refcon)            
  153.     {
  154. #pragma unused (reply,refcon)
  155.     
  156.         DescType saveOpt;
  157.         OSErr    tempErr;
  158.         OSErr    myErr;
  159.         DescType returnedType;
  160.         long     actSize;
  161.         
  162.         saveOpt = kAEAsk; /* the default */
  163.         tempErr = AEGetParamPtr(message,
  164.                                                         keyAESaveOptions,
  165.                                                         typeEnumerated,
  166.                                                         &returnedType,
  167.                                                         (Ptr)&saveOpt,
  168.                                                         sizeof(saveOpt),
  169.                                                         &actSize);
  170.         
  171.         if (saveOpt != kAENo)
  172.             myErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  173.  
  174.       if (myErr == noErr)
  175.             DoQuit(saveOpt);
  176.         
  177.         return(myErr);
  178.     }
  179.  
  180.  
  181. // -----------------------------------------------------------------------
  182. //    Name:             DoPrintDocuments
  183. //    Purpose:        Print a list of documents (or windows).
  184. // -----------------------------------------------------------------------
  185.  
  186. pascal OSErr DoPrintDocuments(const AppleEvent *message, AppleEvent *reply, long refcon)
  187. {
  188. #pragma unused (reply, refcon)
  189.     long          index;
  190.     long          itemsInList;
  191.     AEKeyword     keywd;
  192.     OSErr         err;
  193.     AEDescList    docList;
  194.     Size          actSize;
  195.     DescType      typeCode;
  196.     FSSpec        theFSSpec;
  197.     WindowToken   theWindowToken;
  198.     OSErr         forgetErr;
  199.     Boolean       talkToUser;
  200.         
  201.     err = AEGetParamDesc(message,
  202.                                              keyDirectObject,
  203.                                              typeAEList,
  204.                                              &docList);
  205.                                              
  206.     err = AECountItems(&docList, &itemsInList);
  207.         
  208.     for (index = 1; index<=itemsInList; index++)
  209.         if (err == noErr) 
  210.             {
  211.                 forgetErr = AEGetNthPtr( &docList, index, typeFSS, &keywd,
  212.                                                                  &typeCode, (Ptr)&theFSSpec, sizeof(theFSSpec), &actSize);
  213.                                                                     
  214.                 talkToUser = false;        // (AEInteractWithUser(kAEDefaultTimeout, nil, nil) == noErr);
  215.     
  216.                 if (forgetErr == noErr) 
  217.                     {
  218.                         if (err == noErr) 
  219.                             err = IssueAEOpenDoc(theFSSpec);
  220.                             
  221.                         if (err == noErr) 
  222.                             IssuePrintWindow(FrontWindow(), talkToUser);
  223.                             
  224.                         if (err == noErr) 
  225.                             IssueCloseCommand(FrontWindow());
  226.                     }
  227.                 else
  228.                     { /* wasn't a file - was it a window ? */
  229.                         err = AEGetNthPtr(&docList,
  230.                                           index,
  231.                                           typeMyWndw,
  232.                                           &keywd,
  233.                                                             &typeCode,
  234.                                                             (Ptr)&theWindowToken,
  235.                                                             sizeof(WindowToken),
  236.                                                             &actSize);
  237.                                                                 
  238.                                                                                             
  239.                         if (err == noErr)
  240.                                 PrintWindow(DPtrFromWindowPtr(theWindowToken.tokenWindow), talkToUser);
  241.                     }
  242.             }
  243.     
  244.     if (docList.dataHandle)
  245.         forgetErr = AEDisposeDesc(&docList);
  246.         
  247.     return(err);
  248. } /* DoPrintDocuments */
  249.  
  250.  
  251. // -----------------------------------------------------------------------
  252. //    Name:             InitAppleEvents
  253. //    Purpose:        Initialise the AppleEvent despatch table
  254. // -----------------------------------------------------------------------
  255.  
  256. void    InitAppleEvents(void)
  257. {
  258.     OSErr aevtErr;
  259.     
  260.     gRefNum = CurResFile();        // Needed for getting application property
  261.     
  262.         // set up the dispatch table for the four standard AppleEvents
  263.     
  264.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenApplication, NewAEEventHandlerProc(DoOpenApp), noRefCon, false) ;
  265.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,   NewAEEventHandlerProc(DoOpenDocument), noRefCon, false) ;
  266.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,  NewAEEventHandlerProc(DoPrintDocuments), noRefCon, false) ;
  267.     aevtErr = AEInstallEventHandler( kCoreEventClass, kAEQuitApplication, NewAEEventHandlerProc(MyQuit), noRefCon, false) ;
  268.     
  269.         // set up the dispatch table for the core AppleEvents
  270.  
  271.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAESetData,                 NewAEEventHandlerProc(DoSetData),   noRefCon, false);
  272.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEGetData,         NewAEEventHandlerProc(DoGetData),   noRefCon, false);
  273.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEGetDataSize,     NewAEEventHandlerProc(DoGetDataSize),   noRefCon, false);
  274.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAECountElements,   NewAEEventHandlerProc(DoCountElements),   noRefCon, false);
  275.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDoObjectsExist,  NewAEEventHandlerProc(DoObjectsExist),   noRefCon, false);
  276.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAECreateElement,   NewAEEventHandlerProc(DoNewElement),   noRefCon, false);
  277.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEClone,           NewAEEventHandlerProc(DoClone),   noRefCon, false);
  278.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEDelete,          NewAEEventHandlerProc(DoDelete),noRefCon, false);
  279.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEMove,                       NewAEEventHandlerProc(DoMove),   noRefCon, false);
  280.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAEClose,           NewAEEventHandlerProc(DoCloseWindow),noRefCon, false);
  281.     aevtErr = AEInstallEventHandler( kAECoreSuite, kAESave,            NewAEEventHandlerProc(DoSaveWindow),noRefCon, false);
  282.  
  283.         // set up the dispatch table for the miscellaneous AppleEvents
  284.     
  285.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECut,    NewAEEventHandlerProc(DoCut),   noRefCon, false);
  286.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAECopy,   NewAEEventHandlerProc(DoCopy),  noRefCon, false);
  287.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAEPaste,  NewAEEventHandlerProc(DoPaste), noRefCon, false);
  288.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAERevert, NewAEEventHandlerProc(DoRevert),noRefCon, false);
  289.     aevtErr = AEInstallEventHandler( kAEMiscStandards, kAESelect, NewAEEventHandlerProc(DoSelect),noRefCon, false);
  290.  
  291.         // Install recording handlers
  292.     aevtErr = InstallRecordingHandlers();
  293.     
  294.         // Install callbacks for count and compare procedures
  295.     aevtErr = InstallObjectCallbacks();
  296.     
  297.         // Now install our object accessors
  298.     aevtErr = InstallAccessors();
  299.  
  300.         // Now the coercion handlers
  301.     aevtErr = InstallCoercions();
  302.  
  303. } // InitAppleEvents
  304.